package neusoft.edu.cn.a215lmeordering.activities;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.fragments.CollectionFragment;
import neusoft.edu.cn.a215lmeordering.fragments.CollectionFragment_1;
import neusoft.edu.cn.a215lmeordering.fragments.CollectionFragment_2;
import neusoft.edu.cn.a215lmeordering.fragments.SearchFragment;
import neusoft.edu.cn.a215lmeordering.fragments.ShopFragment;
import neusoft.edu.cn.a215lmeordering.fragments.UserFragment;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, BottomNavigationView.OnNavigationItemSelectedListener{
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private FragmentPagerAdapter adapter;
    private List<Fragment> fragmentList;
    private int UserId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewPager1);
        bottomNavigationView = findViewById(R.id.bottomNavigationView1);

        UserId = getIntent().getIntExtra("user_id",0);
        Toast.makeText(getApplicationContext(), "UserId --> "+UserId, Toast.LENGTH_SHORT).show();

        fragmentList = new ArrayList<>();
        fragmentList.add(new ShopFragment());
        fragmentList.add(new CollectionFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new UserFragment());

        CollectionFragment_1 collectionFragment_1 = new CollectionFragment_1();
        collectionFragment_1.setUser_id(UserId);

        CollectionFragment_2 collectionFragment_2 = new CollectionFragment_2();
        collectionFragment_2.setUser_id(UserId);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            //根据位置返回对应的Fragment   0对应ShopFragment
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            //返回Fragment的数量
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);

        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        //选中"店铺"导航按钮
        bottomNavigationView.setSelectedItemId(R.id.nav_shop);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

        int id=0;
        switch(i){
            case 0:
                id = R.id.nav_shop;
                break;
            case 1:
                id = R.id.nav_collection;
                break;
            case 2:
                id = R.id.nav_search;
                break;
            case 3:
                id = R.id.nav_user;
                break;
        }
        System.out.println("fanhuiPa:"+id);
        bottomNavigationView.setSelectedItemId(id);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = 0;
        switch (menuItem.getItemId())
        {
            case R.id.nav_shop:
                id=0;
                break;
            case R.id.nav_collection:
                id=1;
                break;
            case R.id.nav_search:
                id=2;
                break;
            case R.id.nav_user:
                id=3;
                break;
        }
        System.out.println("fanhuiNa:"+id);
        viewPager.setCurrentItem(id);
        return true;
    }

}